// pages/course/kcxq/kcxq.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user:'',
    url:'',
    index:0,
    index1:0,
    id:0,
    course:'',
    consultantId:0,
    consultant:'',
    isLoading:false,
    activeQuestionId:null,
    questionQuery:{
        courseId:1,
        pageNum:1,
        pageSize:5
    },
    classQaQuestionList:[],
    
    answerPagesize:5,
    classAnswerList:[],
    answerCount:0,
    questionAnswerList:[],
    orderRule:'',
    questionPageNum:1,
    questionPageSize:5,
    questionPages:0,

    RatingPagesize:5,
    courseRatingList:[],
    courseCollect:'',
    isCollect:false

    
  },
    //列表切换
    changeIndex(e){
      console.log(e)
      this.setData({
        index:e.currentTarget.dataset.index,
        RatingPagesize:5,
        courseRatingList:[],
        
      })
      if(e.currentTarget.dataset.index==4){
        this.getCourseRating();
      }
    },
    //回复列表切换
    changeIndex1(e){
      this.setData({
        index1:e.currentTarget.dataset.index1,
        questionPageNum:1,
        questionPageSize:5,
        questionAnswerList:[]
      })
      //获取所有问题和答案根据index1,排序
      if(this.data.index1==0){
        console.log(this.data.index1,"index1");
        this.setData({
          orderRule:'',
          questionAnswerList:[]
        })
      }else if(this.data.index1==1){
        this.setData({
          orderRule:'last_answer',
          questionAnswerList:[]
        })
      }else{
        this.setData({
          orderRule:'answer_count',
          questionAnswerList:[]
        })
      }
      this.getClassQaQuestionAndAnswer();
    },
    //获取课程详情
    getCourse(){
      wx.showLoading({
        title: '加载中',
      })
      this.setData({
        isLoading:true
      })
      wx.request({
        url: this.data.url+'controller/'+this.data.id,
        success:resp=>{
          this.setData({
            course:resp.data.data,
            consultantId:resp.data.data.consultantId
          })
          console.log(this.data.course)
          // console.log(this.data.consultantId+"获取到的咨询师id信息")
        },
        complete:()=>{
          wx.hideLoading()
          this.setData({
            isLoading:false
          })
          //查询咨询师信息
          this.getConsultant();
           //根据课程id获取所有评价
          this.getCourseRating();
          //获取所有答案
          this.getClassQaQuestionAndAnswer();
          //查询是否收藏和购买
          this.selectisCollect()
        }
      })
    },
    //根据consultantId查询咨询师信息
    getConsultant(){
      if(this.data.isLoading){
        return;
      }
      wx.request({
        url: this.data.url+'consultant/'+this.data.consultantId,
        success:resp=>{
          console.log(resp)
          this.setData({
            consultant:resp.data.data
          })
          console.log(this.data.consultant)
        }
      })
    },

    // 获取课程所有问题和答案
    getClassQaQuestionAndAnswer(){
      console.log(this.data.index1,"index1");
      console.log(this.data.course,"66666");
      wx.request({
            url: this.data.url+'classQaQuestion/getClassQaQuestionByCourseId',
            data:{
              courseId:this.data.course.id,
              orderRule:this.data.orderRule,
              pageNum:this.data.questionPageNum,
              pageSize:this.data.questionPageSize

            },
            success:resp=>{
              console.log(resp.data,"查看页数")
              this.setData({
                questionAnswerList:[...this.data.questionAnswerList,...resp.data.data.list],
                questionPages:resp.data.data.pages
              })
            }
          })
    },
    //查看更多课堂问题，按照最新排列
    getMoreQuestion(){
      console.log(this.data.questionPages);
      this.setData({
        questionPageSize:1000,
        questionAnswerList:[]
      })
      this.getClassQaQuestionAndAnswer()
    },

    //是否显示更多回复
    toggleReply(e){
      const questionId=e.currentTarget.dataset.questionId;
      if(this.data.user){
        this.setData({
          activeQuestionId:this.data.activeQuestionId==questionId?null:questionId
        })
      }else{
            // 未登录，显示提示弹窗
            wx.showModal({
              title: '提示',
              content: '您尚未登录，请先登录',
              cancelText: '取消',
              confirmText: '去登录',
              success: (res) => {
                if (res.confirm) {
                  // 用户点击"去登录"，跳转到登录页面
                  wx.navigateTo({
                    url: '/pages/my/login/login'
                  })
                } else if (res.cancel) {
                  // 用户点击"取消"，不做任何操作
                  console.log('用户取消登录')
                }
              }
            })
      }
      
    },

    //根据课程id查询所有评价
    getCourseRating(){
      console.log(this.data.course,"55555555");
      wx.request({
        url: this.data.url+'courseRating',
        data:{
          courseId:this.data.course.id,
          pageNum:1,
          pageSize:this.data.RatingPagesize
        },
        success:resp=>{
          console.log(resp);

          this.setData({
            courseRatingList:resp.data.data.list
          })
        }
      })
    },
    //查询是否收藏,和购买
    selectisCollect(){
      wx.request({
        url: this.data.url+'courseCollect',
        method:'GET',
        data:{
          userId:this.data.user.id,
          courseId:this.data.course.id,
        },
        success:resp=>{
          console.log(resp,9999)
          if(resp.data.data!=null){
            this.setData({
              courseCollect:resp.data.data,
              isCollect:true
            })
          }else{
            this.setData({
              isCollect:false
            })
          }
        }
      })
    },

    //收藏课程，并加积分(先不加)
    collectCouse(){
      console.log(this.data.user,"用户");

      if(this.data.user){
          //没有收藏可以收藏
      if(!this.data.isCollect){
        wx.request({
          url: this.data.url+'courseCollect',
          method:"POST",
          data:{
            courseId:this.data.course.id,
            userId:this.data.user.id
          },
          success:resp=>{
            console.log();
            if(resp.data.code==200){
              wx.showToast({
                title: '收藏成功',
                icon:'none'
              })
              this.setData({
                isCollect:true
              })
              this.getCourse()
            }
          }
        })

      }else{
        //取消收藏
        wx.request({
          url: this.data.url+'courseCollect',
          method:"DELETE",
          data:{
            courseId:this.data.course.id,
            userId:this.data.user.id
          },
          success:resp=>{
            if(resp.data.code==200){
              wx.showToast({
                title: '取消收藏',
                icon:'none'
              })
              this.setData({
                isCollect:false
              })
              this.getCourse()
            }
          }
        })
      }
      }else{
            // 未登录，显示提示弹窗
        wx.showModal({
          title: '提示',
          content: '您尚未登录，请先登录',
          cancelText: '取消',
          confirmText: '去登录',
          success: (res) => {
            if (res.confirm) {
              // 用户点击"去登录"，跳转到登录页面
              wx.navigateTo({
                url: '/pages/my/login/login'
              })
            } else if (res.cancel) {
              // 用户点击"取消"，不做任何操作
              console.log('用户取消登录')
            }
          }
        })
      }

    

      
    },
    //查看所有评价
    lookAllRating(){
      this.setData({
        RatingPagesize:1000
      })
      this.getCourseRating()
    },
    toKctw(e){
      const courseId=e.currentTarget.dataset.courseid
      console.log(e.currentTarget.dataset.courseid)
      if(this.data.user){
        wx.navigateTo({
          url: '/pages/course/zxkc/kcxq/kctw/kctw?courseId='+courseId,
        })
      }else{
            // 未登录，显示提示弹窗
        wx.showModal({
          title: '提示',
          content: '您尚未登录，请先登录',
          cancelText: '取消',
          confirmText: '去登录',
          success: (res) => {
            if (res.confirm) {
              // 用户点击"去登录"，跳转到登录页面
              wx.navigateTo({
                url: '/pages/my/login/login'
              })
            } else if (res.cancel) {
              // 用户点击"取消"，不做任何操作
              console.log('用户取消登录')
            }
          }
        })
      }
    },
    toKcPj(e){
      const courseId=e.currentTarget.dataset.courseid
      console.log(e.currentTarget.dataset.courseid)
      if(this.data.user){
        wx.navigateTo({
          url: '/pages/course/zxkc/kcxq/kcpj/kcpj?courseId='+courseId,
        })
      }else{
            // 未登录，显示提示弹窗
        wx.showModal({
          title: '提示',
          content: '您尚未登录，请先登录',
          cancelText: '取消',
          confirmText: '去登录',
          success: (res) => {
            if (res.confirm) {
              // 用户点击"去登录"，跳转到登录页面
              wx.navigateTo({
                url: '/pages/my/login/login'
              })
            } else if (res.cancel) {
              // 用户点击"取消"，不做任何操作
              console.log('用户取消登录')
            }
          }
        })
      }
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let courseId=options.id;
    if(!courseId){
      courseId=wx.getStorageSync('courseId');
    }else{
      wx.setStorageSync('courseId', courseId);
    }
    this.setData({
      user:wx.getStorageSync('user'),
      url:app.globalData.url,
      id:courseId
    })
    if(courseId){
      this.getCourse();
    }
    console.log(this.data.user);
    //根据课程id获取所有课程信息

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})